package com.magic.smartcity.view.me;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.navigation.Navigation;

import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.magic.smartcity.R;
import com.magic.smartcity.beans.BalanceBean;
import com.magic.smartcity.beans.FeedbackBean;
import com.magic.smartcity.beans.TokenBean;
import com.magic.smartcity.beans.UserBean;
import com.magic.smartcity.databinding.FragmentMeBinding;
import com.magic.smartcity.interfaces.IUserCallBack;
import com.magic.smartcity.interfaces.IUserPresenter;
import com.magic.smartcity.interfaces.base.BaseUserCallBackFragment;
import com.magic.smartcity.utils.Constant;
import com.magic.smartcity.view.MainActivity;

import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MeFragment extends BaseUserCallBackFragment {


    private String mToken;
    private FragmentMeBinding mBinding;
    private SharedPreferences mSharedPreferences;
    @Inject
    IUserPresenter mPresenter;
    private String TAG = "MeFragment";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = FragmentMeBinding.inflate(getLayoutInflater());

    }


    ////////////////  ---  接口回调

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mSharedPreferences = getActivity().getSharedPreferences("login-user", Context.MODE_PRIVATE);
        mToken = mSharedPreferences.getString("token", null);
        return mBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG,"------------------- onResume刷新登录当前用户是否登录的数据 ------------------");
        init();
        initEvent();
    }

    private void init() {
        if (mToken != null && !mToken.trim().equals("")) {
            mPresenter.registerCallBack(this);
            initUserView();
        } else {
            initView();
        }
    }

    private void initEvent() {
        mBinding.toLogin.setOnClickListener(v -> {//跳转登录
            if (mSharedPreferences.getString("token", null) == null) {
                Navigation.findNavController(v).navigate(R.id.action_nav_me_to_loginFragment);
            } else {
                Toast.makeText(getContext(), "已登录，请重新刷新页面", Toast.LENGTH_SHORT).show();
            }
        });

        mBinding.money.setOnClickListener(v -> {//跳转电子钱包
            Navigation.findNavController(v).navigate(R.id.action_nav_me_to_meBalanceListFragment);
        });
        mBinding.list1.setOnClickListener(v -> {//跳转个人信息

            Navigation.findNavController(v).navigate(R.id.action_nav_me_to_editInfoFragment);
        });
//        mBinding.list2.setOnClickListener(v -> {//TODO:跳转订单列表
//
//        });
        mBinding.list3.setOnClickListener(v -> {//跳转修改密码
            Navigation.findNavController(v).navigate(R.id.action_nav_me_to_editPasswordFragment);
        });
        mBinding.list4.setOnClickListener(v -> {//跳转意见反馈
            Navigation.findNavController(v).navigate(R.id.action_nav_me_to_feedbackFragment);
        });
        mBinding.loginOut.setOnClickListener(v -> {//退出登录
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString("token", null);
            edit.apply();
            Constant.TOKEN=null;
            initView();
        });
    }

    /**
     * 未登录状态下
     */
    private void initView() {
        Glide.with(this).load(R.mipmap.ic_launcher)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(mBinding.imageTouxiang);
        mBinding.userList.setVisibility(View.GONE);
        mBinding.loginList.setVisibility(View.VISIBLE);
        mBinding.username.setText("未登录");
        mBinding.score.setText("");
    }

    /**
     * 登录状态下
     */
    private void initUserView() {
        mPresenter.getNowUser();
        mBinding.userList.setVisibility(View.VISIBLE);
        mBinding.loginList.setVisibility(View.GONE);
        Glide.with(this).load(R.mipmap.ic_launcher)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(mBinding.imageTouxiang);
        mBinding.username.setText("已登录");

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mToken != null) {
            mPresenter.unRegisterCallBack(this);
        }
    }

    ////////////////////////////接口回调
    @Override
    public void getUserLoaded(UserBean.UserBeanDo user) {
        Log.d(TAG, "当前登录的用户:::::" + user.getUserName());
        mBinding.username.setText("昵称: " + user.getNickName());
        mBinding.score.setText("积分: " + user.getScore() + "分");
        Glide.with(this).load(user.getAvatar()).error(R.mipmap.ic_launcher)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(mBinding.imageTouxiang);
        setHasOptionsMenu(true);
    }

    @Override
    public void error(String str) {
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }


}